package com.quik.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.text.SimpleDateFormat;
import java.util.List;
import com.quik.common.Constants;
import com.quik.dto.StatDto;
import com.quik.entity.Statistics;
import com.quik.entity.DtaWorker;

/**
 * 功能说明：
 * 		  相关的一些功能函数
 * @author Jenny
 *
 */
public class Tools {
	/**
	 * 日期转化成字符串
	 * @param date
	 * @return 返回yyyy-MM-dd HH:mm:ss
	 * @throws Exception
	 */
	public static String date2String(Date date) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	public static String date2String8(Date date) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(date);
	}
	/**
	 * 获取系统当前日期
	 * 格式：yyyyMMdd 
	 * @return
	 */
	public static String getNowDay(String formatter){
		SimpleDateFormat sdf = new SimpleDateFormat(formatter);
		return sdf.format(new java.util.Date(System.currentTimeMillis()));
	}

	/**
	 * 根据规则生成帐单编码
	 *        账单编号，B+yyyyMMddhh+XXXXXX
	 * @param billingNo
	 * @return
	 */
	public static  String generateBillingNo(Integer billingNo){
		StringBuilder sb = new StringBuilder("B");
		sb.append(Tools.getNowDay("yyyyMMddhh"));
		
		int tempLen = String.valueOf(billingNo).length();
		if(tempLen > 6){
			//长过六位截断
			sb.append(String.valueOf(billingNo).subSequence(tempLen - BILLINGNO_INDEX_LEN, tempLen));
			
		}else{
			//不足六位补零
			for(int i = 0; i < (BILLINGNO_INDEX_LEN - tempLen); i++){
				sb.append("0");
			}
			sb.append(String.valueOf(billingNo));
		}
		
		return sb.toString();
	}
	
	/**
	 * 组合系统表中使用的常量类型
	 * @param tableName
	 * @param cons
	 * @return
	 */
	public static String getSection(String tableName, String cons){
		StringBuilder sb = new StringBuilder("[");
		sb.append(tableName);
		sb.append("]_");
		sb.append(cons);
		return sb.toString();
	}
	
	/**
	 * 统计房间或手牌等的状态数量
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public static List statList(List list) throws Exception{
		StatDto dto = new StatDto();
		List statList = new ArrayList();
		
		for(Iterator its = list.iterator(); its.hasNext();){
			Statistics stat = (Statistics)its.next();
			if(dto.getId() == null){
				dto.setId(stat.getId());
				dto.setName(stat.getName());
				dto.setTotal(stat.getAmount());
				dto.addList(stat);
			}else if(dto.getId() != null){
				if(dto.getId().intValue() == stat.getId().intValue()){
					dto.setTotal(dto.getTotal() + stat.getAmount());
					dto.addList(stat);
				}else{
					statList.add(dto);
					dto = new StatDto();
					dto.setId(stat.getId());
					dto.setName(stat.getName());
					dto.setTotal(stat.getAmount());
					dto.addList(stat);
				}
			}
		}
		statList.add(dto);
		
		return statList;
	}
	
	/**
	 * 格式化金额
	 * @param amount  Y100.00
	 * @return 返回以分为单位
	 * @throws Exception
	 */
	public static Integer getAmount(String amount) throws Exception{
		if(amount == null || amount.length() <= 0)
			return 0;
		
		String nAmount = amount.substring(1, amount.length() - 3) + amount.substring(amount.length()-2);
		nAmount = nAmount.replaceAll(",", "");
		
		return Integer.parseInt(nAmount);
	}
	
	/**
	 * 计算日期（以后的多少小时）
	 * @param hour
	 * @return
	 * @throws Exception
	 */
	public static Date calcDateAfter(Date date, Integer hour) throws Exception{
		long days = date.getTime() + hour * 60 * 60 * 1000;
		return new Date(days);
	}
	
	/**
	 * 计算两个日期之差（分）（备注：不考虑秒）
	 * @param startTime
	 * @param endTime
	 * @return
	 * @throws Exception
	 */
	public static long compareDate(Date startTime, Date endTime) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); //格式化，去掉秒
		long result = sdf.parse(sdf.format(endTime)).getTime() - sdf.parse(sdf.format(startTime)).getTime();
		return result/(1000*60);
		
	}
	
	/**
	 * 计算两个时间之间的天数
	 * @param sTime
	 * @param eTime
	 * @return
	 * @throws Exception
	 */
	public static int calcDiffDate(Date sTime, Date eTime) throws Exception{
		long times = eTime.getTime() - sTime.getTime();
		return (int)times/(1000*60*60*24);
	}
	
	/**
	 * 计算配置的段内段外时间段
	 * @return
	 */
	public static ProductPriceDate configProductTime(String configStart, String configEnd, String startTime, String endTime, int buffTime) throws Exception{
		int start = Integer.parseInt(configStart)/100;
		int end = Integer.parseInt(configEnd)/100;
		
		String sTime = startTime.substring(0, startTime.length() - 3); //去掉秒
		String eTime = endTime.substring(0, endTime.length() - 3);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		
		//计算整点（开始时间是，按60-缓冲。 结束时间是按0+缓冲）
		int s = 60 - 10 - 1;
		
		String[] sTimes = sTime.split(":");
		sTime = sTimes[0] + ":00"; 
		if(Integer.parseInt(sTimes[1]) > s){
			sTime = sdf.format(Tools.calcDateAfter(sdf.parse(sTime), 1));
		}
		System.out.println("开始时间：" + sTime);
		
		
		String[] eTimes = eTime.split(":");
		eTime = eTimes[0] + ":00";
		if(Integer.parseInt(eTimes[1]) > buffTime)
			eTime = sdf.format(Tools.calcDateAfter(sdf.parse(eTime), 1));
		System.out.println("结束时间：" + eTime);
		
		//计算小时
		int inHour = 0;  //时间段内小时
		int outHour = 0; //时间段外小时

		sdf.applyPattern("yyyy-MM-dd");
		
		int diff = Tools.calcDiffDate(sdf.parse(sTime), sdf.parse(eTime));
		
		int sHour = Integer.parseInt(sTime.substring(sTime.length() - 5, sTime.length() - 3));
		int eHour = Integer.parseInt(eTime.substring(eTime.length() - 5, eTime.length() - 3));
		
		if(diff < 1){
			if(sHour >= start){
				if(sHour >= end){
					outHour = eHour - sHour;
				}else{
					if(eHour <= end){
						inHour = eHour - sHour;
					}else{
						inHour = end - sHour;
						outHour = eHour - end;
					}
				}
			}else if(sHour < start){				
				if(eHour <= start){
					outHour = eHour - sHour;
				}else{
					if(eHour <= end){
						outHour = start - sHour;
						inHour = eHour - start;
					}else{
						outHour = start-sHour + eHour - end;
						inHour = end - start;
					}
				}
			}
		}else{

			for(int i = 0; i <= diff; i++){			
				if(i == 0){ //第一天
					if(sHour >= start){
						if(sHour >= end){
							outHour += 24 - sHour;
						}else{
							inHour += end - sHour;
							outHour += 24 - end;
						}
					}else{
						inHour += end - start;
						outHour += start - sHour + 24 - end;
					}
				}else if(i < diff ){
					inHour += end - start;
					outHour += 24 - end + start;
				}else if(i == diff){
					if(eHour >= start){
						if(eHour >= end){
							inHour += end - start;
							outHour += start + eHour - end;
						}else{
							outHour += start;
							inHour += eHour - start;
						}
					}else{
						outHour += eHour;
					}
				}
			}
		}
		ProductPriceDate date = new ProductPriceDate(inHour, outHour);
		
		return date;
	}
	
	/** 帐单编号的长度 **/
	private static final Integer BILLINGNO_INDEX_LEN = 6;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		String startTime = "2014-01-29 15:01:55";
		String endTime = "2014-01-29 18:13:04";
		
		ProductPriceDate date = Tools.configProductTime("800", "1800", startTime, endTime, 5);
		System.out.println(date.getInHour());
		System.out.println(date.getOutHour());
		
	}
}
